$(function(){
	//加载数据
	loadDatas();
	
	//初始化日期控件
	$('#begin_time').datetimepicker({
        format: 'yyyy-mm-dd hh:ii:00',
        language: 'zh-CN',
        autoclose: true,
        minView: "hour",
        minuteStep: 5,
        startDate: new Date()
    }).on("changeDate",function(ev){
    	$('#end_time').datetimepicker('setStartDate', ev.date);
    });
    $('#end_time').datetimepicker({
        format:'yyyy-mm-dd hh:ii:00',
        language: 'zh-CN',
        autoclose: true,
        minView: "hour",
        minuteStep: 5,
        startDate: new Date()
    }).on("changeDate",function(ev){
		$('#begin_time').datetimepicker('setEndDate', ev.date);
    });
});


//数据加载
function loadDatas(){
	
	$.ajax({
		type: "post",
		url: "../../../php/gonggao/loadGongGaoListDatas.php",
		beforeSend: function(){
			$("#load").css("display","block");
		},
		success: function(msg){
			$("#load").css("display","none");
			var datas = msg.records;
			var html = "";
			//表格数据
			if(datas.length==0){
				html += "<tr><td colspan='9'>没查询到符合条件的数据~</td></tr>";
			}else{
				for(var i = 0; i<datas.length; i++){
					html += "<tr>";
					html += "<td>"+datas[i].id+"</td>";
					html += "<td>"+datas[i].title+"</td>";
					
					html += "<td width='350px'>";
					html += "<div class='hidenConent' style='width:300px;' title='"+datas[i].remark+"' onclick='updateGongGao("+datas[i].id+")'>";
					html += datas[i].remark;
					html += "</div>";
					html += "</td>";
					
					html += "<td>"+datas[i].beginTime+"</td>";
					html += "<td>"+datas[i].endTime+"</td>";
					
					html += "<td>";
					html += "<div class='hidenConent' style='width:320px;' title='"+datas[i].imgUrl+"'>"+datas[i].imgUrl+"</div>";
					html += "</td>";
					
					html += "<td>"+datas[i].sort+"</td>";
					if(datas[i].isOnline == "1"){
						html += "<td>已上线</td>";
					}else{
						html += "<td>未上线</td>";
					}
					//操作
					html += "<td style='font-size:16px;'>";
					html += "<span class='fa fa-pencil-square-o' style='cursor: pointer; color:#daa40b;' title='修改' onclick='updateGongGao("+datas[i].id+")'></span>";
					html += "<span class='fa fa-trash' style='margin-left:10px; cursor: pointer; color:#dd5044' title='删除' onclick='deleteGongGao("+datas[i].id+")'></span>";
					html += "</td>";
					html += "</tr>";
				}
			}
			
			$("#databody").html(html);
		},
		error: function(msg){
			alert("连接出错！");
		}
	});
}

//加载要修改的数据
function updateGongGao(id){
	$.ajax({
		type: "post",
		data: {"id": id},
		url: "../../../php/gonggao/loadGongGaoById.php",
		beforeSend: function(){
			$("#load").css("display","block");
		},
		success: function(msg){
			$("#load").css("display","none");
			var gongGaoObj = msg.records[0];
			
//			console.log(gongGaoObj);
			
			$("#id").val(gongGaoObj.id);
			$("#ggTitle").val(gongGaoObj.title);
			$("#ggContent").val(gongGaoObj.remark);
			$("#ggBegin").val(gongGaoObj.beginTime);
			$("#ggEnd").val(gongGaoObj.endTime);
			$("#imgUrl").val(gongGaoObj.imgUrl);
			$("#sortNo").val(gongGaoObj.sort);
			var on = gongGaoObj.isOnline;			
			$("#isOnline option[value="+on+"]").attr("selected","selected");
			
			$("#editGongGaoWindow").modal("show");
		}
	});
}

//删除公告确定框
function deleteGongGao(id){
	$("#delId").val(id);
	$("#confirmWindow").modal("show");
}
//提交删除
function subDel(){
	var id = $("#delId").val();
	$.ajax({
		type: "post",
		data: {"id": id},
		url: "../../../php/gonggao/delGongGaoById.php",
		beforeSend: function(){
			cannelDel();//初始化确定框
			$("#load").css("display","block");
		},
		success: function(msg){
			$("#load").css("display","none");
			if(msg == "1"){
				loadDatas();//重新加载数据
			}else{
				alert(msg);
			}
		}
	});
}
//取消删除
function cannelDel(){
	$("#delId").val("");
	$("#confirmWindow").modal("hide");
}

//提交公告编辑
function subEditGongGao(){
	var id = $("#id").val();//id
	var title = $.trim($("#ggTitle").val());//标题
	var content = $.trim($("#ggContent").val());//内容
	var begin = $("#ggBegin").val();//开始时间
	var end = $("#ggEnd").val();//结束时间
	var imgUrl = $.trim($("#imgUrl").val());//图片地址
	var sortNo = $("#sortNo").val();//排序序号
	var isOnline = $("#isOnline").val();//是否在线
	//判断标题
	if(title == ""){
		$("#ggTitle").css("border","1px solid #dd5044");
		$("#title_error").css("display","block");
	}else{
		$("#ggTitle").css("border","");
		$("#title_error").css("display","none");
	}
	//判断内容
	if(content == ""){
		$("#ggContent").css("border","1px solid #dd5044");
		$("#content_error").css("display","block");
	}else{
		$("#ggContent").css("border","");
		$("#content_error").css("display","none");
	}
	//判断时间
	if(begin == "" && end == ""){
		$("#begin_time").css("border","1px solid #dd5044");
		$("#time_error").css("display","block");
		$("#end_time").css("border","1px solid orange");
		$("#time_tishi").css("display","none");
	}else if(begin == "" && end != ""){
		$("#begin_time").css("border","1px solid #dd5044");
		$("#time_error").css("display","block");
		$("#end_time").css("border","");
		$("#time_tishi").css("display","none");
	}else if(begin != "" && end == ""){
		$("#begin_time").css("border","");
		$("#time_error").css("display","none");
		$("#end_time").css("border","1px solid orange");
		$("#time_tishi").css("display","block");
	}else{
		$("#begin_time, #end_time").css("border","");
		$("#time_error, #time_tishi").css("display","none");
	}
	//判断图片地址
	if(imgUrl == ""){
		$("#imgUrl").css("border","1px solid #dd5044");
		$("#url_error").css("display","block");
	}else{
		$("#imgUrl").css("border","");
		$("#url_error").css("display","none");
	}
	//排序序号
	if(sortNo == ""){
		$("#sortNo").css("border","1px solid #dd5044");
		$("#sort_error").css("display","block");
	}else{
		$("#sortNo").css("border","");
		$("#sort_error").css("display","none");
	}
	//是否在线
	if(isOnline == ""){
		$("#isOnline").css("border","1px solid #dd5044");
		$("#online_error").css("display","block");
	}else{
		$("#isOnline").css("border","");
		$("#online_error").css("display","none");
	}
	
	if(title != "" && content != "" && begin != "" && imgUrl != "" && sortNo != "" && isOnline != ""){
		var editUrl = "";
		if(id == ""){//id为空时，添加公告
			editUrl = "../../../php/gonggao/addGongGao.php";
		}else{//id不为空时，编辑公告
			editUrl = "../../../php/gonggao/updateGongGao.php";
		}
		if(end == ""){
			end = "2099-01-01 00:00:00";
		}
//		alert(editUrl);
		if(editUrl != ""){
			$.ajax({
				type: "post",
				data: {
					"id": id,
					"title": title,
					"content": content,
					"begin": begin,
					"end": end,
					"imgUrl": imgUrl,
					"sortNo": sortNo,
					"isOnline": isOnline
				},
				url: editUrl,
				beforeSend: function(){
					$("#load").css("display","block");
				},
				success: function(msg){
					$("#load").css("display","none");
//					console.log(msg);
					if(msg == "1"){
						cannelEdit();//初始化编辑框
						loadDatas();//重新加载数据
					}else{
						alert(msg);
					}
				}
			});
		}else{
			alert("请求错误，请稍后重试！");
		}
	}
	
}

//取消编辑公告
function cannelEdit(){
	$("#editGongGaoWindow").modal("hide");
	$("#resetGGForm").click();
	$("#id").val("");
	$("#ggTitle, #ggContent, #begin_time, #end_time, #imgUrl, #sortNo, #isOnline").css("border","");
	$(".fa-remove, .fa-exclamation").css("display","none");
}
